
function GenericComponentFactory(ctor, params) {
  this._ctor = ctor;
  this._params = params;
}
GenericComponentFactory.prototype = {
  _ctor: null,
  _params: null,

  createInstance: function GCF_createInstance(outer, iid) {
    if (outer != null)
      throw Cr.NS_ERROR_NO_AGGREGATION;
    return (new this._ctor(this._params)).QueryInterface(iid);
  },

  QueryInterface: function GCF_QueryInterface(iid) {
    if (iid.equals(Components.interfaces.nsIFactory) ||
        iid.equals(Components.interfaces.nsISupports))
      return this;
    throw Cr.NS_ERROR_NO_INTERFACE;
  }
};


function MyService() {
   this._count = 0;
   this.wrappedJSObject = this;
}

MyService.prototype.ref = function() {
   this._count++;
}

MyService.prototype.__defineGetter__("count", function() {
   return this._count;
});

MyService.prototype.QueryInterface = function(iid) {
   if (!iid.equals(Components.interfaces.nsISupports)) {
      throw Components.results.NS_ERROR_NO_INTERFACE;
   }
   return this;
}


var id = "@milowski.com/xmlapp-sample-service;1";
if (!Components.classes[id]) {
   Console.log("Registering");
   var CID = Components.ID("{352D7266-F68F-4F94-ABCF-A36369BB2B81}");
   var cr = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar);
   cr.registerFactory(CID,"XML App Sample Service",id,new GenericComponentFactory(MyService));
}

var serviceClass = Components.classes[id];
var service = serviceClass.getService(Components.interfaces.nsISupports).wrappedJSObject;
service.ref();
Console.log("Service count: "+service.count);





